﻿Imports System.Data.OleDb

Public Class frmSuaPhiVanChuyen
    Public IDDonHangBan As Integer = 0
    Private aMaKhachHang As New AutoCompleteStringCollection
    Private Sub frmSuaPhiVanChuyen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            AutoComplete_MaKhachHang()
            comboTien_Load()
            NhanVienTiepThi_Load()
            GenerateOrderNumber()
            InitOrder()
            DonHangBan_Load()
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub
    Public Sub AutoComplete_MaKhachHang()
        Try
            'Auto Complete 
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtCustomerInfo As New DataTable
            Dim query As String = "Select CodeKhachHang From DanhMucKhachHang Where Xoa=0"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtCustomerInfo)
            dbConn.Close()
            aMaKhachHang = New AutoCompleteStringCollection
            For i As Integer = 0 To dtCustomerInfo.Rows.Count - 1
                aMaKhachHang.Add(dtCustomerInfo.Rows(i)("CodeKhachHang"))
            Next
            txtMaKhachHang.AutoCompleteMode = AutoCompleteMode.SuggestAppend
            txtMaKhachHang.AutoCompleteSource = AutoCompleteSource.CustomSource
            txtMaKhachHang.AutoCompleteCustomSource = aMaKhachHang
        Catch ex As Exception

        End Try
    End Sub
    Private Sub NhanVienTiepThi_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsNhanVienTiepThi As New DataSet
            Dim dtNhanVienTiepThi As New DataTable
            Dim query As String = "Select NhanVienTiepThiId,TenNhanVienTiepThi From NhanVienTiepThi Union Select pkey as NhanVienTiepThi,[name] as TenNhanVienTiepThi From KhongXacDinh Order By NhanVienTiepThiId ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsNhanVienTiepThi)
            dbConn.Close()
            dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
            cbxNhanVienTT.DataSource = dtNhanVienTiepThi
            cbxNhanVienTT.ValueMember = "NhanVienTiepThiId"
            cbxNhanVienTT.DisplayMember = "TenNhanVienTiepThi"
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub DisableTiepThi()
        cbxNhanVienTT.Enabled = False
        txtDaiLy.Enabled = False
    End Sub

    Private Sub rbtNhanVienTiepThi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtNhanVienTiepThi.CheckedChanged
        Try
            DisableTiepThi()
            cbxNhanVienTT.Enabled = True
            txtDaiLy.Text = ""
            lblDaiLyId.Text = "0"
        Catch ex As Exception

        End Try
    End Sub
    Private Sub rbtDaiLy_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtDaiLy.CheckedChanged
        Try
            DisableTiepThi()
            txtDaiLy.Enabled = True
        Catch ex As Exception

        End Try
    End Sub
    Private Sub comboTien_Load()
        Try
            Dim Tien As New List(Of String)
            Tien.Add("VND")
            Tien.Add("USD")
            Tien.Add("SGD")
            comboTien.DataSource = Tien
            comboTien.SelectedIndex = 0
            'set index
        Catch ex As Exception

        End Try
    End Sub
    Protected Function InitOrderTable() As DataTable
        Dim dtTable As DataTable = New DataTable()
        Try
            'RowIndex 
            Dim RowIndex As DataColumn = New DataColumn("RowIndex")
            RowIndex.DataType = System.Type.GetType("System.Int32")
            dtTable.Columns.Add(RowIndex)

            Dim IDSanPham As DataColumn = New DataColumn("IDSanPham")
            IDSanPham.DataType = System.Type.GetType("System.Int32")
            dtTable.Columns.Add(IDSanPham)

            Dim MaSanPham As DataColumn = New DataColumn("MaSanPham")
            MaSanPham.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(MaSanPham)

            Dim TenSanPham As DataColumn = New DataColumn("TenSanPham")
            TenSanPham.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TenSanPham)

            Dim TenQuyCach As DataColumn = New DataColumn("TenQuyCach")
            TenQuyCach.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TenQuyCach)

            Dim SoLuong As DataColumn = New DataColumn("SoLuong")
            SoLuong.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(SoLuong)

            Dim DonGia As DataColumn = New DataColumn("DonGia")
            DonGia.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(DonGia)

            Dim IsManyPrice As DataColumn = New DataColumn("IsManyPrice")
            IsManyPrice.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(IsManyPrice)

            Dim ThanhTien As DataColumn = New DataColumn("ThanhTien")
            ThanhTien.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThanhTien)

            Dim LoaiPhi As DataColumn = New DataColumn("LoaiPhi")
            LoaiPhi.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(LoaiPhi)

            Return dtTable
        Catch ex As Exception
            Throw
        End Try
    End Function
    Private Sub GenerateOrderNumber()
        Try
            Dim dtNoOrder As New DataTable
            Dim NoOrder As DataRow
            dtNoOrder.Columns.Add("STT")
            For i As Integer = 0 To 11
                NoOrder = dtNoOrder.NewRow
                NoOrder.Item("STT") = i + 1
                dtNoOrder.Rows.Add(NoOrder)
                dtNoOrder.AcceptChanges()
            Next
            dgvNoOrder.DataSource = dtNoOrder
            dgvNoOrder.ClearSelection()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub InitOrder()
        Try
            'dtSanPhamHD = CreateSanPhamHopDong()
            Dim dtOrder As New DataTable
            dtOrder = InitOrderTable()
            Dim newRow As DataRow
            For i As Integer = 0 To 11
                newRow = dtOrder.NewRow
                newRow.Item("RowIndex") = i + 1
                newRow.Item("IDSanPham") = 0
                newRow.Item("MaSanPham") = ""
                newRow.Item("TenSanPham") = ""
                newRow.Item("TenQuyCach") = ""
                newRow.Item("SoLuong") = ""
                newRow.Item("DonGia") = ""
                newRow.Item("ThanhTien") = ""
                newRow.Item("LoaiPhi") = ""
                newRow.Item("IsManyPrice") = ""
                dtOrder.Rows.Add(newRow)
            Next
            dgvSanPhamHopdong_Tab1.AutoGenerateColumns = False
            dgvSanPhamHopdong_Tab1.DataSource = dtOrder
        Catch ex As Exception
            Throw
        End Try
    End Sub
    Private Sub DonHangBan_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            'Trạng Thái Đơn Hàng 
            Dim TrangThai As String
            dbCommand = New OleDbCommand("spDonHangBan_TrangThai", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@IDDonHangBan", OleDbType.Integer).Value = IDDonHangBan
            TrangThai = dbCommand.ExecuteScalar()
            If TrangThai = "Đã duyệt" Then
                cbxTruKho.Checked = True
            ElseIf TrangThai = "Chưa duyệt" Then
                cbxTruKho.Checked = False
                'DaTruKho = False
            End If
            '
            'Thong Tin Don Hang spDonHangBan_DonHang
            Dim dtDonHang As New DataTable
            dbCommand = New OleDbCommand("spDonHangBan_DonHang", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@IDDonHangBan", OleDbType.VarWChar).Value = IDDonHangBan
            Dim adapter As OleDb.OleDbDataAdapter
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtDonHang)
            'Thong Tin Khach Hang
            txtHoTenNguoiMua.Text = dtDonHang.Rows(0)("TenNguoiMua")
            txtTenKhachHang.Text = dtDonHang.Rows(0)("TenKhachHang")
            txtDiaChi.Text = dtDonHang.Rows(0)("DiaChi")
            txtMaSoThue.Text = dtDonHang.Rows(0)("MaSoThue")
            dtpNgayHopDong.Value = dtDonHang.Rows(0)("NgayBan")
            txtMaKhachHang.Text = dtDonHang.Rows(0)("CodeKhachHang")
            lblKhachHangId.Text = dtDonHang.Rows(0)("KhachHangId")
            txtHinhThucThanhToan.Text = dtDonHang.Rows(0)("HinhThucThanhToan")
            txtSo.Text = dtDonHang.Rows(0)("So")
            cbxNoPrintMoney.Checked = IIf(dtDonHang.Rows(0)("HoaDonTang") = 1, True, False)
            txtPhiVanChuyen.Value = dtDonHang.Rows(0)("PhiVanChuyen")
            txtGhiChu.Text = IIf(IsDBNull(dtDonHang.Rows(0)("GhiChu")), "", dtDonHang.Rows(0)("GhiChu"))
            cbxTinhDoanhThu.Checked = dtDonHang.Rows(0)("TinhDoanhThu")
            'tien
            If dtDonHang.Rows(0)("Tien") = "VND" Then
                comboTien.SelectedIndex = 0
            ElseIf dtDonHang.Rows(0)("Tien") = "USD" Then
                comboTien.SelectedIndex = 1
            Else : dtDonHang.Rows(0)("Tien") = "SGD"
                comboTien.SelectedIndex = 2
            End If

            'Kiem Tra Tiep Thi
            If dtDonHang.Rows(0)("HinhThucTiepThi") = "Đại lý" Then
                rbtDaiLy.Checked = True
                txtDaiLy.Enabled = True
                rbtNhanVienTiepThi.Checked = False
                cbxNhanVienTT.Enabled = False
                txtDaiLy.Text = dtDonHang.Rows(0)("NhanVienTiepThi")
                lblDaiLyId.Text = dtDonHang.Rows(0)("NhanVienTiepThiId")
            Else
                rbtDaiLy.Checked = False
                txtDaiLy.Enabled = False
                rbtNhanVienTiepThi.Checked = True
                cbxNhanVienTT.Enabled = True
                cbxNhanVienTT.SelectedValue = dtDonHang.Rows(0)("NhanVienTiepThiId")
            End If
            txtVat.Text = dtDonHang.Rows(0)("ThueVat")
            'Thong Tin Chi Tiet Don Hang spDonHangBan_ChiTietDonHang
            'khoi tao lai
            Dim dtSanPhamHD As New DataTable
            dtSanPhamHD = InitOrderTable()
            Dim newRow As DataRow
            Dim dtChiTietDonHang As New DataTable
            dbCommand = New OleDbCommand("spDonHangBan_ChiTietDonHang", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@IDDonHangBan", OleDbType.Integer).Value = IDDonHangBan
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtChiTietDonHang)
            For i As Integer = 0 To dtChiTietDonHang.Rows.Count - 1
                newRow = dtSanPhamHD.NewRow
                newRow.Item("IDSanPham") = dtChiTietDonHang.Rows(i)("IdSanPham")
                newRow.Item("MaSanPham") = dtChiTietDonHang.Rows(i)("MaSanPham")
                newRow.Item("TenSanPham") = dtChiTietDonHang.Rows(i)("TenSanPham")
                newRow.Item("TenQuyCach") = dtChiTietDonHang.Rows(i)("TenQuyCach")
                newRow.Item("SoLuong") = dtChiTietDonHang.Rows(i)("SoLuong")
                newRow.Item("DonGia") = dtChiTietDonHang.Rows(i)("DonGia")
                newRow.Item("ThanhTien") = dtChiTietDonHang.Rows(i)("ThanhTien")
                newRow.Item("LoaiPhi") = dtChiTietDonHang.Rows(i)("LoaiPhi")
                newRow.Item("IsManyPrice") = dtChiTietDonHang.Rows(i)("IsManyPrice")
                dtSanPhamHD.Rows.Add(newRow)
            Next
            'Phat Sinh Dong Trang
            Dim iRowEmpty As Integer = 12 - dtChiTietDonHang.Rows.Count - 1
            For i As Integer = 0 To iRowEmpty
                newRow = dtSanPhamHD.NewRow
                newRow.Item("IDSanPham") = 0
                newRow.Item("MaSanPham") = ""
                newRow.Item("TenSanPham") = ""
                newRow.Item("TenQuyCach") = ""
                newRow.Item("SoLuong") = ""
                newRow.Item("DonGia") = ""
                newRow.Item("ThanhTien") = ""
                newRow.Item("LoaiPhi") = ""
                dtSanPhamHD.Rows.Add(newRow)
            Next
            dtSanPhamHD.DefaultView.Sort = "LoaiPhi DESC"
            dgvSanPhamHopdong_Tab1.DataSource = dtSanPhamHD
            dbConn.Close()
            TinhTienHangChuaThue()
            TinhTienThueVAT()
            TinhTienThueDaThue()
        Catch ex As Exception

        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Tạo số hóa đơn
    ''' </summary>
    ''' <remarks>
    ''' 
    ''' </remarks>
    ''' <history>
    '''   [lamtn] 19/02/2011  Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub TinhTienHangChuaThue()
        Try
            'Dim ThanhTienHangHoa As Integer = 0
            'fix VND,USD,SGD 
            Dim ThanhTienHangHoa As Double
            For i As Integer = 0 To dgvSanPhamHopdong_Tab1.Rows.Count - 1
                Dim IDSanPham As Integer = Integer.Parse(dgvSanPhamHopdong_Tab1("IDSanPham", i).Value)
                Dim LoaiPhi As String = dgvSanPhamHopdong_Tab1("LoaiPhi", i).Value
                'Dim iThanhTien As Integer
                Dim iThanhTien As Double
                If IDSanPham > 0 And LoaiPhi = "HangHoa" Then
                    iThanhTien = dgvSanPhamHopdong_Tab1("ThanhTien", i).Value
                    ThanhTienHangHoa = ThanhTienHangHoa + iThanhTien
                End If
            Next
            'fix VND,USD,SGD 
            'Dim ThanhTienGiamTru As Integer = 0
            Dim ThanhTienGiamTru As Double = 0
            For i As Integer = 0 To dgvSanPhamHopdong_Tab1.Rows.Count - 1
                Dim IDSanPham As Integer = Integer.Parse(dgvSanPhamHopdong_Tab1("IDSanPham", i).Value)
                Dim LoaiPhi As String = dgvSanPhamHopdong_Tab1("LoaiPhi", i).Value
                'fix VND,USD,SGD 
                'Dim iThanhTien As Integer
                Dim iThanhTien As Double
                If IDSanPham > 0 And LoaiPhi = "GiamTru" Then
                    iThanhTien = dgvSanPhamHopdong_Tab1("ThanhTien", i).Value
                    ThanhTienGiamTru = ThanhTienGiamTru + iThanhTien
                End If
            Next
            'Dim CongTienHang As Integer = ThanhTienHangHoa - ThanhTienGiamTru
            'fix VND,USD,SGD 
            Dim CongTienHang As Double = ThanhTienHangHoa - ThanhTienGiamTru
            txtCongTienHang.Text = CongTienHang.ToString("N02", US)
        Catch ex As Exception
            Throw
        End Try
    End Sub
    Private Sub TinhTienThueVAT()
        Try
            Dim TienThueVAT As Double
            Dim CongTienHang As Double
            Dim VAT As Double = txtVat.Value
            If txtCongTienHang.Text = "" Then
                CongTienHang = 0
            Else
                CongTienHang = Double.Parse(txtCongTienHang.Text)
            End If
            If comboTien.SelectedValue = "VND" Then
                TienThueVAT = LamTron((CongTienHang * VAT) / 100, 0)
            ElseIf comboTien.SelectedValue = "USD" Or comboTien.SelectedValue = "SGD" Then
                TienThueVAT = LamTron((CongTienHang * VAT) / 100, 2)
            End If
            txtTienThueVAT.Text = TienThueVAT.ToString("N02", US)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub TinhTienThueDaThue()
        Try
            Dim TienThueVAT As Double
            Dim CongTienHang As Double
            Dim TongThanhToan As Double
            If txtCongTienHang.Text = "" Then
                CongTienHang = 0
            Else
                CongTienHang = Double.Parse(txtCongTienHang.Text)
            End If
            If txtTienThueVAT.Text = "" Then
                TienThueVAT = 0
            Else
                TienThueVAT = Double.Parse(txtTienThueVAT.Text)
            End If
            TongThanhToan = TienThueVAT + CongTienHang
            If comboTien.SelectedValue = "VND" Then
                TongThanhToan = LamTron(TongThanhToan)
            ElseIf comboTien.SelectedValue = "USD" Or comboTien.SelectedValue = "SGD" Then
                TongThanhToan = LamTron(TongThanhToan, 2)
            End If
            txtTongThanhToan.Text = TongThanhToan.ToString("N02", US)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub cmdLuuPhiVanChuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            'Trạng Thái Đơn Hàng 
            Dim query = "Update DonHangBan Set PhiVanChuyen=" & txtPhiVanChuyen.Value & " Where IDDonHangBan=" & IDDonHangBan
            dbCommand = New OleDbCommand(query, dbConn)
            Dim dl As DialogResult
            dl = MessageBox.Show("Bạn muốn cập nhật phí vận chuyển cho đơn hàng này", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
            If dl = Windows.Forms.DialogResult.OK Then
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
            Else
                dbConn.Close()
                Exit Sub
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrev.Click
        Try
            'IdDonHangBan_Edit
            'IsViewHistory = True
            Dim SoHienTai As Integer = Integer.Parse(txtSo.Text)
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim IDDonHangBan_Preview As Integer
            Dim dbCommand As OleDb.OleDbCommand
            dbCommand = New OleDbCommand("Order_Prev", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.AddWithValue("@SoHoaDon", SoHienTai)
            IDDonHangBan_Preview = dbCommand.ExecuteScalar()
            dbConn.Close()
            If IDDonHangBan_Preview = 0 Then
                MessageBox.Show("Đơn hàng đã hết.Vui lòng nhấp chọn nút Hủy bỏ hoặc nhấn Ctrl+H để nhập đơn hàng", "Thông báo", MessageBoxButtons.OK)
            Else
                Order_ViewByNumber(IDDonHangBan_Preview)
                cmdLuu.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
        Try
            Dim SoHienTai As Integer = Integer.Parse(txtSo.Text)
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim IDDonHangBan_Preview As Integer
            Dim dbCommand As OleDb.OleDbCommand
            dbCommand = New OleDbCommand("Order_Next", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.AddWithValue("@SoHoaDon", SoHienTai)
            IDDonHangBan_Preview = dbCommand.ExecuteScalar()
            dbConn.Close()
            If IDDonHangBan_Preview = 0 Then
                MessageBox.Show("Đơn hàng đã hết.Vui lòng nhấp chọn nút Hủy bỏ nhập đơn hàng", "Thông báo", MessageBoxButtons.OK)
            Else
                Order_ViewByNumber(IDDonHangBan_Preview)
                cmdLuu.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Chỉnh sửa thông tin hóa đơn
    ''' </summary>
    ''' <remarks>
    ''' 
    ''' </remarks>
    ''' <history>
    '''   [lamtn] 18/02/2011  Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub Order_ViewByNumber(ByVal IdDonHangBan As Integer)
        Try
            'check tình trạng, nếu đơn hàng đã duyệt thì không thể xóa
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim query As String
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim TrangThai As String = ""
            Dim dbCommand As OleDb.OleDbCommand
            query = "Select TrangThai From DonHangBan Where IdDonHangBan=" & IdDonHangBan
            dbCommand = New OleDbCommand(query, dbConn)
            TrangThai = dbCommand.ExecuteScalar()
            '
            'TabControl1.SelectedTab = cmdXemThongTinChiTiet
            'show info for edit
            'Dim NgayBan As String = dtpTheoNgay.Value.ToString.Split(" ")(0)
            query = "Select * From DonHangBan Where IDDonHangBan=" & IdDonHangBan
            Dim dsThongKe As New DataSet
            Dim adapter As OleDb.OleDbDataAdapter
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsThongKe)

            'Thong Tin Khach Hang
            txtSo.Text = dsThongKe.Tables(0).Rows(0)("So")
            txtHoTenNguoiMua.Text = dsThongKe.Tables(0).Rows(0)("TenNguoiMua")
            txtTenKhachHang.Text = dsThongKe.Tables(0).Rows(0)("TenKhachHang")
            lblKhachHangId.Text = dsThongKe.Tables(0).Rows(0)("KhachHangId")
            txtDiaChi.Text = dsThongKe.Tables(0).Rows(0)("DiaChi")
            txtMaSoThue.Text = dsThongKe.Tables(0).Rows(0)("MaSoThue")
            dtpNgayHopDong.Value = dsThongKe.Tables(0).Rows(0)("NgayBan")
            txtMaKhachHang.Text = dsThongKe.Tables(0).Rows(0)("CodeKhachHang")
            txtHinhThucThanhToan.Text = dsThongKe.Tables(0).Rows(0)("HinhThucThanhToan")
            'Kiem Tra Tiep Thi
            If dsThongKe.Tables(0).Rows(0)("HinhThucTiepThi") = "Đại lý" Then
                rbtDaiLy.Checked = True
                rbtNhanVienTiepThi.Checked = False
                txtDaiLy.Text = dsThongKe.Tables(0).Rows(0)("NhanVienTiepThi")
                lblDaiLyId.Text = dsThongKe.Tables(0).Rows(0)("NhanVienTiepThiId")
            Else
                rbtDaiLy.Checked = False
                rbtNhanVienTiepThi.Checked = True
                cbxNhanVienTT.SelectedValue = dsThongKe.Tables(0).Rows(0)("NhanVienTiepThiId")
            End If
            txtVat.Text = dsThongKe.Tables(0).Rows(0)("ThueVat")
            'Thong Tin Don Hang
            'khoi tao lai
            Dim dtSanPhamHD As New DataTable
            dtSanPhamHD = InitOrderTable()
            Dim newRow As DataRow
            Dim dsDonHang As New DataSet
            Dim dtDonHang As New DataTable
            query = "Select * From ChiTietDonHangBan Where IdDonHangBan=" & IdDonHangBan
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsDonHang)
            dtDonHang = dsDonHang.Tables(0)
            Dim strThanhTien As String
            For i As Integer = 0 To dtDonHang.Rows.Count - 1
                newRow = dtSanPhamHD.NewRow
                newRow.Item("RowIndex") = i + 1
                newRow.Item("IDSanPham") = dtDonHang.Rows(i)("IdSanPham")
                newRow.Item("MaSanPham") = dtDonHang.Rows(i)("MaSanPham")
                newRow.Item("TenSanPham") = dtDonHang.Rows(i)("TenSanPham")
                newRow.Item("TenQuyCach") = dtDonHang.Rows(i)("TenQuyCach")
                newRow.Item("SoLuong") = dtDonHang.Rows(i)("SoLuong")
                newRow.Item("DonGia") = dtDonHang.Rows(i)("DonGia")
                'fortmatthanhtien
                strThanhTien = CDec(dtDonHang.Rows(i)("ThanhTien")).ToString("N1", Globalization.CultureInfo.CreateSpecificCulture("en-us"))
                newRow.Item("ThanhTien") = strThanhTien
                newRow.Item("LoaiPhi") = dtDonHang.Rows(i)("LoaiPhi")
                dtSanPhamHD.Rows.Add(newRow)
            Next
            'Phat Sinh Dong Trang
            Dim iRowEmpty As Integer = 15 - dtDonHang.Rows.Count - 1
            For i As Integer = 0 To iRowEmpty
                newRow = dtSanPhamHD.NewRow
                newRow.Item("IDSanPham") = 0
                newRow.Item("MaSanPham") = ""
                newRow.Item("TenSanPham") = ""
                newRow.Item("TenQuyCach") = ""
                newRow.Item("SoLuong") = ""
                newRow.Item("DonGia") = ""
                newRow.Item("ThanhTien") = ""
                newRow.Item("LoaiPhi") = ""
                dtSanPhamHD.Rows.Add(newRow)
            Next
            dtSanPhamHD.DefaultView.Sort = "LoaiPhi DESC"
            dgvSanPhamHopdong_Tab1.AutoGenerateColumns = False
            dgvSanPhamHopdong_Tab1.DataSource = dtSanPhamHD


            TinhTienHangChuaThue()
            TinhTienThueVAT()
            TinhTienThueDaThue()
            dbConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub cmdXoaThongTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoaThongTin.Click
        Try
            cmdLuu.Enabled = True
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdIn.Click
        Try
            lblUpdate.Visible = True
            Order_Save()
            Order_Print()
            DonHangBan_Load()
            lblUpdate.Visible = False
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLuu.Click
        Try
            lblUpdate.Visible = True
            Order_Save()
            DonHangBan_Load()
            lblUpdate.Visible = False
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Order_Save()
        Try
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            Dim query As String = ""
            dataAccess.ConnectDatabase_DBAccess(dbConn)

            'KhachHangID 
            dbCommand = New OleDbCommand("spKhachHangID", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@TenKhachHang", OleDbType.VarWChar).Value = txtTenKhachHang.Text
            dbCommand.Parameters.Add("@CodeKhachHang", OleDbType.VarWChar).Value = txtMaKhachHang.Text
            lblKhachHangId.Text = dbCommand.ExecuteScalar()
            'Kiểm tra hình thức tiếp thị
            Dim HinhThucTiepThi As String
            Dim NhanVienTiepThiId As String
            Dim TenNhanVienTiepThi As String
            If rbtNhanVienTiepThi.Checked Then
                'Nhan Vien Tiep Thi
                HinhThucTiepThi = "Nhân viên tiếp thị"
                NhanVienTiepThiId = cbxNhanVienTT.SelectedValue
                TenNhanVienTiepThi = cbxNhanVienTT.Text
            ElseIf rbtDaiLy.Checked Then
                HinhThucTiepThi = "Đại lý"
                NhanVienTiepThiId = lblDaiLyId.Text
                TenNhanVienTiepThi = txtDaiLy.Text
            End If
            Dim TinhDoanhThu As Integer = Integer.Parse(cbxTinhDoanhThu.CheckValue)
            query = String.Format("Update DonHangBan Set KhachHangId={0},CodeKhachHang=N'{1}',TenKhachHang=N'{2}',DiaChi=N'{3}',MaSoThue=N'{4}',HinhThucThanhToan=N'{5}',NhanVienTiepThi=N'{6}',NhanVienTiepThiID={7},TenNguoiMua=N'{8}',PhiVanChuyen={9},NgaySua=N'{10}',NguoiSua=N'{11}',GhiChu=N'{12}',HinhThucTiepThi=N'{13}',TinhDoanhThu={14} Where IDDonHangBan={15}", lblKhachHangId.Text, txtMaKhachHang.Text, txtTenKhachHang.Text, txtDiaChi.Text, txtMaSoThue.Text, txtHinhThucThanhToan.Text, TenNhanVienTiepThi, NhanVienTiepThiId, txtHoTenNguoiMua.Text.Trim, txtPhiVanChuyen.Value, NgayTao, NguoiTao, txtGhiChu.Text, HinhThucTiepThi, TinhDoanhThu, IDDonHangBan)
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            query = "Update ChiTietDonHangBan Set TinhDoanhThu = " & TinhDoanhThu & "  Where IDDonHangBan=" & IDDonHangBan
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Order_Print()
        Try
            Dim objReport_KhongTien As New rptHoaDon_KhongTien
            Dim objReport As New rptHoaDon
            'Lấy được chi tiết đơn hàng bán
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtChiTietDonHangBan As New DataTable
            Dim query As String
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim IDDonHangBan_Print As Integer = 0
            query = "Select Top 1 IDDonHangBan From DonHangBan Where So='" & txtSo.Text & "' And Xoa=0 And TrangThai<>N'Đã hủy'"
            dbCommand = New OleDbCommand(query, dbConn)
            IDDonHangBan_Print = dbCommand.ExecuteScalar()
            query = "Select MaSanPham,TenSanPham,TenQuyCach,SoLuong,DonGia AS DonGiaDec,Cast(ThanhTien As VarChar(50)) As ThanhTien,LoaiPhi From ChiTietDonHangBan Where IdDonHangBan=" & IDDonHangBan & " Order By LoaiPhi DESC"
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtChiTietDonHangBan)
            dtChiTietDonHangBan.Columns.Add("DonGia")

            'Upgrade To Increase Perform
            'Tính Tổng tiền hàng
            Dim TongTienHangHoa As Double = 0
            Dim TongTienGiamTru As Double = 0
            Dim TongTienHang As Double = 0
            Dim ThueSuatVAT As Double = 0
            Dim ThueVAT As Double = 0
            TongTienHangHoa = TinhTongTienHangHoa(dtChiTietDonHangBan)
            TongTienGiamTru = TinhTongTienGiamTru(dtChiTietDonHangBan)
            query = "Select ThueVAT From DonHangBan Where IdDonHangBan=" & IDDonHangBan
            dbCommand = New OleDbCommand(query, dbConn)
            ThueSuatVAT = dbCommand.ExecuteScalar()
            TongTienHang = TongTienHangHoa - TongTienGiamTru
            If comboTien.SelectedValue = "VND" Then
                ThueVAT = LamTron((ThueSuatVAT * TongTienHang) / 100, 0)
            ElseIf comboTien.SelectedValue = "USD" Or comboTien.SelectedValue = "SGD" Then
                ThueVAT = LamTron((ThueSuatVAT * TongTienHang) / 100, 3)
            End If

            '----Cat chuyen
            'Lấy Thông Tin Tiền Hàng
            Dim dtThongTinTienHang As New DataTable
            dtThongTinTienHang = ThongTinTienDonHang()
            Dim newRow As DataRow
            newRow = dtThongTinTienHang.NewRow
            Dim TongTienThanhToan As Double = 0
            TongTienThanhToan = TongTienHang + ThueVAT
            newRow.Item("TienHang") = TongTienHang.ToString("N", VietNam)
            newRow.Item("ThueGTGT") = ThueVAT.ToString("N", VietNam)
            newRow.Item("ThueSuatGTGT") = ThueSuatVAT '.ToString
            newRow.Item("TongThanhToan") = TongTienThanhToan.ToString("N", VietNam)
            Dim doctien As String = ""
            If comboTien.SelectedValue = "VND" Then
                doctien = Num2Text(LamTron(TongTienThanhToan, 0))
            ElseIf comboTien.SelectedValue = "USD" Then
                'Tách ra phần USD và phần cents
                Dim Tien As String = LamTron(TongTienThanhToan, 2).ToString()
                Dim aTien() As String = Tien.Split(".")
                Dim TienChan As Integer
                Dim TienLe As Integer
                If aTien.Length = 2 Then
                    TienChan = Integer.Parse(aTien(0))
                    TienLe = Integer.Parse(aTien(1))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " USD " & Num2Text(TienLe).Replace("đồng", "").ToLower() & " cents"
                ElseIf aTien.Length = 1 Then
                    TienChan = Integer.Parse(aTien(0))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " USD"
                End If
            ElseIf comboTien.SelectedValue = "SGD" Then
                'Tách ra phần USD và phần cents
                Dim Tien As String = LamTron(TongTienThanhToan, 2).ToString()
                Dim aTien() As String = Tien.Split(".")
                Dim TienChan As Integer
                Dim TienLe As Integer
                If aTien.Length = 2 Then
                    TienChan = Integer.Parse(aTien(0))
                    TienLe = Integer.Parse(aTien(1))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " SGD " & Num2Text(TienLe).Replace("đồng", "").ToLower() & " cents"
                ElseIf aTien.Length = 1 Then
                    TienChan = Integer.Parse(aTien(0))
                    doctien = Num2Text(TienChan).Replace("đồng", "") & " SGD"
                End If
            End If
            newRow.Item("TienBangChu") = doctien
            dtThongTinTienHang.Rows.Add(newRow)
            If cbxNoPrintMoney.Checked Then
                objReport_KhongTien.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
            Else
                objReport.Database.Tables("ThongTinTienDonHang").SetDataSource(dtThongTinTienHang)
            End If

            Dim DonGia As Double
            For i As Integer = 0 To dtChiTietDonHangBan.Rows.Count - 1
                If dtChiTietDonHangBan.Rows(i)("LoaiPhi") = "GiamTru" Then
                    DonGia = Double.Parse(dtChiTietDonHangBan.Rows(i)("DonGiaDec"))
                    If DonGia <= 100 Then
                        dtChiTietDonHangBan.Rows(i)("TenSanPham") = dtChiTietDonHangBan.Rows(i)("MaSanPham") & " (" & DonGia & "%)"
                    Else
                        dtChiTietDonHangBan.Rows(i)("TenSanPham") = dtChiTietDonHangBan.Rows(i)("MaSanPham")
                    End If
                    dtChiTietDonHangBan.Rows(i)("DonGia") = ""
                Else
                    dtChiTietDonHangBan.Rows(i)("DonGia") = Convert.ToDouble(dtChiTietDonHangBan.Rows(i)("DonGiaDec")).ToString("N", VietNam).ToString()
                End If
                'format dongia
                dtChiTietDonHangBan.Rows(i)("ThanhTien") = Convert.ToDouble(dtChiTietDonHangBan.Rows(i)("ThanhTien")).ToString("N", VietNam).ToString()
            Next
            ''Phát Sinh các row trắng
            Dim emptyRow As DataRow
            For i As Integer = 1 To 12 - dtChiTietDonHangBan.Rows.Count
                emptyRow = dtChiTietDonHangBan.NewRow
                dtChiTietDonHangBan.Rows.Add(emptyRow)
                'dtChiTietDonHangBan.AcceptChanges()
            Next
            If cbxNoPrintMoney.Checked Then
                objReport_KhongTien.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
            Else
                objReport.Database.Tables("ChiTietDonHangBan").SetDataSource(dtChiTietDonHangBan)
            End If
            'Thông tin khách hàng
            Dim dtThongTinKhachHang As New DataTable
            query = "Select * From DonHangBan Where IDDonHangBan=" & IDDonHangBan
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtThongTinKhachHang)
            If dtThongTinKhachHang.Rows(0)("NhanVienTiepThi") = "Chưa có" Then
                dtThongTinKhachHang.Rows(0)("NhanVienTiepThi") = ""
            End If
            If cbxNoPrintMoney.Checked Then
                objReport_KhongTien.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)
            Else
                objReport.Database.Tables("ThongTinKhachHang").SetDataSource(dtThongTinKhachHang)
            End If
            'Thông Tin Ngày Hóa Đơn 
            Dim dsNgay As New dsNgay
            Dim dtNgay As New DataTable
            dtNgay = dsNgay.Tables(0)
            Dim dateRow As DataRow
            dateRow = dtNgay.NewRow
            dateRow.Item("Ngay") = dtpNgayHopDong.Value.ToShortDateString()
            formatDateVN2String_In(dateRow.Item("Ngay"))
            dtNgay.Rows.Add(dateRow)
            If cbxNoPrintMoney.Checked Then
                objReport_KhongTien.Database.Tables("dtNgay").SetDataSource(CType(dtNgay, DataTable))
            Else
                objReport.Database.Tables("dtNgay").SetDataSource(CType(dtNgay, DataTable))
            End If
            If cbxNoPrintMoney.Checked Then
                objReport_KhongTien.PrintOptions.PrinterName = Printer()
                objReport_KhongTien.PrintToPrinter(1, False, 1, 1)
            Else
                objReport.PrintOptions.PrinterName = Printer()
                objReport.PrintToPrinter(1, False, 1, 1)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    'Hàm Tính Tổng Tiền Hàng Hóa Trong Hóa Đơn
    Private Function TinhTongTienHangHoa(ByVal dtSanPham As DataTable) As Double
        Try
            Dim sRet As Double = 0
            Dim rowHangHoa() As DataRow
            rowHangHoa = dtSanPham.Select("LoaiPhi='HangHoa'")
            For i As Integer = 0 To rowHangHoa.Length - 1
                sRet = sRet + rowHangHoa(i).Item("ThanhTien")
            Next
            Return sRet
        Catch ex As Exception
            ShowError()
        End Try
    End Function
    'Hàm Tính Tổng Tiền Giảm Trừ Trong Hóa Đơn
    Private Function TinhTongTienGiamTru(ByVal dtSanPham As DataTable) As Double
        Try
            Dim sRet As Double = 0
            Dim rowGiamTru() As DataRow
            rowGiamTru = dtSanPham.Select("LoaiPhi='GiamTru'")
            For i As Integer = 0 To rowGiamTru.Length - 1
                'sRet = sRet + Single.Parse(rowGiamTru(i).Item("ThanhTien").ToString, Globalization.NumberStyles.Currency)
                sRet = sRet + rowGiamTru(i).Item("ThanhTien")
            Next
            Return sRet
        Catch ex As Exception
            ShowError()
        End Try
    End Function
    Protected Function ThongTinTienDonHang() As DataTable
        Dim dtTable As DataTable = New DataTable()
        Try

            Dim TienHang As DataColumn = New DataColumn("TienHang")
            TienHang.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TienHang)

            Dim ThueSuatGTGT As DataColumn = New DataColumn("ThueSuatGTGT")
            ThueSuatGTGT.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThueSuatGTGT)

            Dim ThueGTGT As DataColumn = New DataColumn("ThueGTGT")
            ThueGTGT.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThueGTGT)

            Dim TongThanhToan As DataColumn = New DataColumn("TongThanhToan")
            TongThanhToan.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TongThanhToan)

            Dim TienBangChu As DataColumn = New DataColumn("TienBangChu")
            TienBangChu.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TienBangChu)
            Return dtTable
        Catch ex As Exception
            Throw
        End Try
    End Function
#Region "Dai Ly"
    Private Sub txtDaiLy_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDaiLy.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                txtDaiLy.Text = dgvDaiLy("TenDaiLy", dgvDaiLy.CurrentCell.RowIndex).Value
                lblDaiLyId.Text = dgvDaiLy("DaiLyId", dgvDaiLy.CurrentCell.RowIndex).Value
                Panel_DaiLy.Visible = False
                dgvSanPhamHopdong_Tab1.Focus()
            End If
        Catch ex As Exception

        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Xử lý sự kiện khi đánh các phím chữ vào txtDaiLy Thi Show Danh Sách Đại Lý
    ''' </summary>
    ''' <remarks>
    ''' 
    ''' </remarks>
    ''' <history>
    '''   [lamtn] 18/02/2011  Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    ''' 
    Private Sub txtDaiLy_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDaiLy.KeyUp
        Try
            If txtDaiLy.Text <> "" Then
                DaiLy_Search()
                If e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
                    dgvDaiLy.Focus()
                ElseIf e.KeyCode = Keys.Enter Then
                    Panel_DaiLy.Visible = False
                End If
            Else
                Panel_DaiLy.Visible = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Xử lý sự kiện Show Danh Sách Đại Lý
    ''' </summary>
    ''' <remarks>
    ''' 
    ''' </remarks>
    ''' <history>
    '''   [lamtn] 18/02/2011  Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    ''' 
    Private Sub DaiLy_Search()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsKhachHang As New DataSet
            Dim dtKhachHang As New DataTable
            Dim query As String = "Select KhachHangId,MaKhachHang,TenKhachHang,SoDienThoai,DiaChi From DanhMucKhachHang Where TenKhachHang Like N'%" & txtDaiLy.Text.Trim & "%'"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsKhachHang)
            dbConn.Close()
            dtKhachHang = dsKhachHang.Tables(0)
            Panel_DaiLy.Visible = True
            dgvDaiLy.AutoGenerateColumns = False
            dgvDaiLy.DataSource = dtKhachHang
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
    Private Sub dgvDaiLy_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvDaiLy.KeyDown
        Try
            Select Case e.KeyCode
                Case Keys.Down
                    Panel_DaiLy.Visible = True
                Case Keys.Up
                    Panel_DaiLy.Visible = True
                Case Keys.Enter
                    txtDaiLy.Text = dgvDaiLy("TenDaiLy", dgvDaiLy.CurrentCell.RowIndex).Value
                    lblDaiLyId.Text = dgvDaiLy("DaiLyId", dgvDaiLy.CurrentCell.RowIndex).Value
                    Panel_DaiLy.Visible = False
                    dgvSanPhamHopdong_Tab1.Focus()
                Case Else
                    txtDaiLy.Focus()
            End Select
        Catch ex As Exception

        End Try
    End Sub
#End Region
#Region "Thêm Khách Hàng"
    Sub KhachHang_Quick_Search()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsKhachHang As New DataSet
            Dim dtKhachHang As New DataTable
            Dim query As String = "Select KhachHangId,MaKhachHang,TenKhachHang,SoDienThoai,DiaChi From DanhMucKhachHang Where Xoa=0 And TenKhachHang Like N'%" & txtTenKhachHang.Text.Trim & "%'"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsKhachHang)
            dbConn.Close()
            dtKhachHang = dsKhachHang.Tables(0)
            Panel_KhachHang.Visible = True
            dgv_KhachHang_QuickSearch.AutoGenerateColumns = False
            dgv_KhachHang_QuickSearch.DataSource = dtKhachHang
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub txtTenKhachHang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenKhachHang.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                lblKhachHangId.Text = dgv_KhachHang_QuickSearch.Rows(dgv_KhachHang_QuickSearch.CurrentCell.RowIndex).Cells("KhachHangId_Search").Value.ToString()
                '---------Lấy Thống Tin Khách Hàng----------
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dsTinhThanh As New DataSet
                Dim dtTinhThanh As New DataTable
                Dim query As String = "Select * From DanhMucKhachHang Where KhachHangId=" & lblKhachHangId.Text
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dsTinhThanh)

                dtTinhThanh = dsTinhThanh.Tables(0)
                '
                txtMaKhachHang.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("CodeKhachHang")), "", dtTinhThanh.Rows(0)("CodeKhachHang"))
                txtTenKhachHang.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("TenKhachHang")), "", dtTinhThanh.Rows(0)("TenKhachHang"))
                txtDiaChi.Text = dtTinhThanh.Rows(0)("DiaChi")
                txtMaSoThue.Text = IIf(IsDBNull(dtTinhThanh.Rows(0)("MaSoThue")), "", dtTinhThanh.Rows(0)("MaSoThue"))
                cbxNhanVienTT.SelectedValue = Integer.Parse(IIf(IsDBNull(dtTinhThanh.Rows(0)("NhanVienTiepThiId")), 0, dtTinhThanh.Rows(0)("NhanVienTiepThiId").ToString))
                Panel_KhachHang.Visible = False
                dbConn.Close()
                txtHinhThucThanhToan.Focus()
            ElseIf e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then

            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub txtTenKhachHang_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenKhachHang.KeyUp
        Try
            If txtTenKhachHang.Text <> "" Then
                KhachHang_Quick_Search()
                If e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
                    dgv_KhachHang_QuickSearch.Focus()
                    Panel_KhachHang.Visible = True
                End If
            Else
                Panel_KhachHang.Visible = False
                lblKhachHangId.Text = "0"
                txtDiaChi.Text = ""
                txtMaSoThue.Text = ""
                txtMaKhachHang.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
#End Region
#Region "Auto Complete"
    Private Sub txtMaKhachHang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMaKhachHang.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                'Truyen MaKhachHang De Lay Thong Tin
                Dim dtKhachHangInfo As New DataTable
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim adapter As OleDb.OleDbDataAdapter
                Dim dbCommand As OleDb.OleDbCommand
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand("spKhachHang_Item", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@KhachHangId", OleDbType.Integer).Value = 0
                dbCommand.Parameters.Add("@CodeKhachHang", OleDbType.VarWChar).Value = txtMaKhachHang.Text.Trim
                dbCommand.Parameters.Add("@Type", OleDbType.Integer).Value = 1
                adapter = New OleDb.OleDbDataAdapter(dbCommand)
                adapter.Fill(dtKhachHangInfo)
                dbConn.Close()
                If dtKhachHangInfo.Rows.Count > 0 Then
                    lblKhachHangId.Text = dtKhachHangInfo.Rows(0)("KhachHangId")
                    txtMaKhachHang.Text = IIf(IsDBNull(dtKhachHangInfo.Rows(0)("CodeKhachHang")), "", dtKhachHangInfo.Rows(0)("CodeKhachHang"))
                    txtTenKhachHang.Text = IIf(IsDBNull(dtKhachHangInfo.Rows(0)("TenKhachHang")), "", dtKhachHangInfo.Rows(0)("TenKhachHang"))
                    txtDiaChi.Text = dtKhachHangInfo.Rows(0)("DiaChi")
                    txtMaSoThue.Text = IIf(IsDBNull(dtKhachHangInfo.Rows(0)("MaSoThue")), "", dtKhachHangInfo.Rows(0)("MaSoThue"))
                    cbxNhanVienTT.SelectedValue = Integer.Parse(IIf(IsDBNull(dtKhachHangInfo.Rows(0)("NhanVienTiepThiId")), 0, dtKhachHangInfo.Rows(0)("NhanVienTiepThiId").ToString))
                    Panel_KhachHang.Visible = False
                    txtHinhThucThanhToan.Focus()
                Else
                    txtMaKhachHang.Focus()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub 
#End Region
End Class